package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.CellMaterialTimezoneSettingDao;
import com.wing.cabinet.model.entity.CellMaterialTimezoneSetting;
import com.wing.cabinet.model.request.CellMaterialTimezoneSettingForm;
import com.wing.cabinet.model.response.CellMaterialTimezoneSettingVO;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.service.ProductDetailService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 物料时区设置-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CellMaterialTimezoneSettingServiceImpl extends ServiceImpl<CellMaterialTimezoneSettingDao, CellMaterialTimezoneSetting> implements CellMaterialTimezoneSettingService {

    @Autowired
    private ProductDetailService productDetailService;

    @Override
    public int add(CellMaterialTimezoneSettingForm cellMaterialTimezoneSettingForm) {
        int result = 0;
        ProductDetail productDetail = productDetailService.getById(cellMaterialTimezoneSettingForm.getSkuId());
        CellMaterialTimezoneSetting selectOne = baseMapper.selectOne(new LambdaQueryWrapper<CellMaterialTimezoneSetting>()
                .eq(CellMaterialTimezoneSetting::getSkuCode, productDetail.getSkuCode())
                .eq(CellMaterialTimezoneSetting::getEnterpriseCode,JwtUtils.getEnterpriseCode()));
        if(selectOne==null){
            //校验
            CellMaterialTimezoneSetting cellMaterialTimezoneSetting =new CellMaterialTimezoneSetting();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(CellMaterialTimezoneSetting.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(cellMaterialTimezoneSettingForm, cellMaterialTimezoneSetting, copyOptions);
            cellMaterialTimezoneSetting.setSkuCode(productDetail.getSkuCode());
            cellMaterialTimezoneSetting.setEnterpriseCode(JwtUtils.getEnterpriseCode());
            result = baseMapper.insert(cellMaterialTimezoneSetting);
        }else {
            selectOne.setStartDay(cellMaterialTimezoneSettingForm.getStartDay());
            selectOne.setEndDay(cellMaterialTimezoneSettingForm.getEndDay());
            result = baseMapper.updateById(selectOne);
        }
        return result;
    }

    @Override
    public List<CellMaterialTimezoneSettingVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CellMaterialTimezoneSettingVO> pageList4app(Page page, Map<String, Object> params) {
        List<CellMaterialTimezoneSettingVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CellMaterialTimezoneSettingVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
